// Copyright (C) Kumo inc. and its affiliates.
// Author: Jeff.li lijippy@163.com
// All rights reserved.
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published
// by the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program.  If not, see <https://www.gnu.org/licenses/>.
//
#pragma once

#include <cstdint>
#include <pollux/exec/aggregate.h>
#include <pollux/exec/aggregate_info.h>
#include <pollux/exec/row_container.h>
#include <pollux/vector/complex_vector.h>

namespace kumo::pollux::exec {
    /// Computes aggregations over de-duplicated inputs. Supports aggregations with
    /// single (e.g. sum, count) or multiple (e.g. covar_pop) input columns.
    class DistinctAggregations {
    public:
        /// @param aggregates Non-empty list of
        /// aggregates that require inputs to be de-duplicated. All
        /// aggregates should have the same inputs.
        /// @param inputType Input row type for the aggregation operator.
        /// @param pool Memory pool.
        static std::unique_ptr<DistinctAggregations> create(
            std::vector<AggregateInfo *> aggregates,
            const RowTypePtr &inputType,
            memory::MemoryPool *pool);

        virtual ~DistinctAggregations() = default;

        virtual Accumulator accumulator() const = 0;

        /// Aggregate-like APIs to aggregate input rows per group.
        void setAllocator(HashStringAllocator *allocator) {
            allocator_ = allocator;
        }

        void setOffsets(
            int32_t offset,
            int32_t nullByte,
            uint8_t nullMask,
            int32_t initializedByte,
            uint8_t initializedMask,
            int32_t rowSizeOffset) {
            offset_ = offset;
            nullByte_ = nullByte;
            nullMask_ = nullMask;
            initializedByte_ = initializedByte;
            initializedMask_ = initializedMask;
            rowSizeOffset_ = rowSizeOffset;
        }

        // Initializes null flags and accumulators for newly encountered groups.  This
        // function should be called only once for each group.
        //
        // @param groups Pointers to the start of the new group rows.
        // @param indices Indices into 'groups' of the new entries.
        void initializeNewGroups(
            char **groups,
            melon::Range<const vector_size_t *> indices) {
            initializeNewGroupsInternal(groups, indices);

            for (auto index: indices) {
                groups[index][initializedByte_] |= initializedMask_;
            }
        }

        virtual void addInput(
            char **groups,
            const RowVectorPtr &input,
            const SelectivityVector &rows) = 0;

        virtual void addSingleGroupInput(
            char *group,
            const RowVectorPtr &input,
            const SelectivityVector &rows) = 0;

        /// Computes aggregations and stores results in the specified 'result' vector.
        virtual void extractValues(
            melon::Range<char **> groups,
            const RowVectorPtr &result) = 0;

    protected:
        // Initializes null flags and accumulators for newly encountered groups.  This
        // function should be called only once for each group.
        //
        // @param groups Pointers to the start of the new group rows.
        // @param indices Indices into 'groups' of the new entries.
        virtual void initializeNewGroupsInternal(
            char **groups,
            melon::Range<const vector_size_t *> indices) = 0;

        HashStringAllocator *allocator_;
        int32_t offset_;
        int32_t nullByte_;
        uint8_t nullMask_;
        int32_t initializedByte_;
        uint8_t initializedMask_;
        int32_t rowSizeOffset_;
    };
} // namespace kumo::pollux::exec
